package com.huaxin.hxcloud.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Entity.ScStationColumnDTO;
import com.huaxin.common.Entity.ScStationColumnFirst;
import com.huaxin.common.Entity.ScStationColumnSecond;
import com.huaxin.common.Entity.StationColumnDTO;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.tools.Log;
import com.huaxin.hxcloud.R;
import com.huaxindata.common.util.HxLogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author songxf
 * @date 2018/5/18
 * @Desc 首创站点树形结构
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxScStationTreeListActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.rv_station_target)
    RecyclerView mRvStationTarget;
    ArrayList<MultiItemEntity> data = new ArrayList<>();
    private HxScStaionListAdapter mHxScStaionListAdapter;
    private View                  emptyView;

    @Override
    protected int getContentViewId() {
        return R.layout.activty_sc_station_list;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        setSupportActionBar(mToolbar);
        mHxScStaionListAdapter = new HxScStaionListAdapter(data, this);
        mRvStationTarget.setAdapter(mHxScStaionListAdapter);
        mRvStationTarget.setLayoutManager(new LinearLayoutManager(this));
        //空布局
        emptyView = LayoutInflater.from(this).inflate(R.layout.item_empty_nodata_center, (ViewGroup) mRvStationTarget.getParent(), false);
    }

    @Override
    protected void requestData() {
        apiNotStationOrgList();
    }

    private void apiNotStationOrgList() {
        updateLoadStatus(LOADING);
        Call<ScStationColumnDTO> call = HxTransmitRequestHelper.getRequestApi().getFindNotStationOrgList();
        autoClose(call);
        call.enqueue(new Callback<ScStationColumnDTO>() {

            @Override
            public void onResponse(Call<ScStationColumnDTO> call, Response<ScStationColumnDTO> response) {
                ScStationColumnDTO stationTargetDTO = response.body();
                if (response.isSuccessful() && stationTargetDTO != null) {
                    updateLoadStatus(LOAD_SUCCESS);
                    List<ScStationColumnFirst> stationTarList = stationTargetDTO.getOrgList();
                    if (stationTarList != null && stationTarList.size() > 0) {
                        //数据初始化
                        updateView(stationTarList);
                    } else {
                        mHxScStaionListAdapter.setEmptyView(emptyView);
                    }

                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<ScStationColumnDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    private void updateView(List<ScStationColumnFirst> stationTarList) {
        data.clear();
        for (int i = 0; i < stationTarList.size(); i++) {
            //第一层
            ScStationColumnFirst stationlist = stationTarList.get(i);
            //第二层
            List<ScStationColumnSecond> stationColumn = stationlist.getChildren();

            if (stationColumn != null && stationColumn.size() > 0) {
                for (int j = 0; j < stationColumn.size(); j++) {
                    data.add(ColumnData(stationColumn.get(j), 0));
                }
            }
        }
        if (data.size() == 0) {
            //设置空布局
            mHxScStaionListAdapter.setEmptyView(emptyView);
        } else {
            mHxScStaionListAdapter.setNewData(data);
        }
    }

    private ScStationColumnSecond ColumnData(ScStationColumnSecond stationColumn, int level) {
        //第二层
        stationColumn.setLevel(level);
        HxLogUtils.i("--stationColumn-->" + stationColumn.toString());
        List<ScStationColumnSecond> stationColumnNextList = stationColumn.getChildren();
        if (stationColumnNextList != null && stationColumnNextList.size() > 0) {
            if (level == 0) {
                stationColumn.setItemType(mHxScStaionListAdapter.ColumnStationFirst);
            } else {
                stationColumn.setItemType(mHxScStaionListAdapter.ColumnStation);
            }
            for (int j = 0; j < stationColumnNextList.size(); j++) {
                ScStationColumnSecond stationColumnNext = stationColumnNextList.get(j);
                //  stationColumnNext.setLevel(level);
                stationColumnNext = ColumnData(stationColumnNext, level + 1);
                stationColumn.addSubItem(stationColumnNext);
            }
        } else {
            if (level == 0) {
                stationColumn.setItemType(mHxScStaionListAdapter.ColumnStationFirst);
            } else if (level == 1) {
                stationColumn.setItemType(mHxScStaionListAdapter.ColumnStation);
            } else {
                stationColumn.setItemType(mHxScStaionListAdapter.ColumnStationEnd);
            }
        }
        Log.i("lwjing", "ColumnData level= " + level + " " + stationColumn.getItemType() + "---id--" + stationColumn.getId());
        return stationColumn;
    }
}
